package zj.thread;

import android.os.Handler;
import android.os.Message;

import org.json.JSONObject;

import zj.http.ZjHttpRequest;
import zj.pojo.ZjObject;
import zj.utils.ZjFinal;

/**
 * Created by xq17 on 2017/5/29.
 */

public class ZjUIThread extends Thread {
    Handler handler;
    int what;
    ZjObject inObject;
    Class<?> outClass;

    public ZjUIThread(Handler handler, ZjObject inObject, Class<?> outClass, int what) {
        this.handler = handler;
        this.what = what;
        this.inObject = inObject;
        this.outClass = outClass;
    }

    @Override
    public void run() {
        super.run();
        Message msg1= new Message();
        msg1.what = ZjFinal.HANDLE_START;
        handler.sendMessage(msg1);

        JSONObject jsonObject = ZjHttpRequest.post(inObject);
        try {
            ZjObject outObject = (ZjObject) outClass.newInstance();
            outObject.JsonObjectToObject(jsonObject);
            Message msg2= new Message();
            msg2.what = what;
            msg2.obj = outObject;
            handler.sendMessage(msg2);
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        Message msg3= new Message();
        msg3.what = ZjFinal.HANDLE_STOP;
        handler.sendMessage(msg3);
    }
}
